﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComponentData
{
    public List<ValueBase> Values=new List<ValueBase>();
    public Type ComponentType;

    public UInt32 CompIdentity;
    public UInt16 EntityPrefabID;
/*    public UInt32 EntityIdentity;

    /// <summary>
    /// 此组件编号
    /// </summary>
    public UInt16 Identity;*/

}

public static class ComponentDataExtensions
{
    static ICompDataManager manager=null;

    public static void AddIgnorePair(this EntityData e,ushort eid)
    {
        manager.AddIgnorePair(e.EntityIdentity.right(), eid);
    }
    public static bool IsIgnored(this EntityData e,ushort eid)
    {
        return manager.IsIgnored(e.EntityIdentity.right(), eid);
    }
    public static void DelIgnorePair(this EntityData e, ushort eid)
    {
        manager.DelIgnorePair(e.EntityIdentity.right(), eid);
    }

    public static EntityData GetEntity(this ComponentData d)
    {
        return manager.GetEntityData(d.CompIdentity.left());
    }
    public static void SetManager(ICompDataManager m)
    {
        Debug.Assert(manager == null);
        manager = m;
    }
    public static void DelManager()
    {
        Debug.Assert(manager!=null);
        manager = null;
    }

    public static UInt16 GetValueCnt(this ComponentData d)
    {
        return (ushort)EntityUtilities.GetValueCnt(d.GetType());
    }

    public static string GetValueName(this ComponentData d,UInt16 id)
    {
        return EntityUtilities.GetValueName(d.GetType(), id);
    }
    public static object GetValue(this ComponentData c,Type type, UInt16 id)
    {
        var tp = c.Values.Find(s => s.ID == id);
        if (tp == null)
        {
            return EntityUtilities.GetDefaultValue(type, c.EntityPrefabID.combine(c.CompIdentity.right()), id);
        }
        else return c.Values.Find(s => s.ID == id).Data;
    }
    #region ValueChange
    public static T GetValue<T>(this ComponentData c, UInt16 id)
    {
        return (T)c.GetValue(typeof(T), id);
    }

    public static T GetValue<T>(this ComponentData c, string valueName)
    {
        return c.GetValue<T>(EntityUtilities.GetValueID(c, valueName));

    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="c"></param>
    /// <param name="id"></param>
    /// <param name="value"></param>
    /// <returns>能否成功设置</returns>
    public static bool SetValue<T>(this ComponentData c, UInt16 id, T value,bool isRequest=false)
    {
       /* if (c.ComponentType == typeof(SCollider))
        {
            Debug.Log(EntityUtilities.GetValueName(c.ComponentType, id) + ":" + value);
        }*/
        //   T oldValue = c.GetValue<T>((ushort)(id));

        var tp = c.Values.Find(s => s.ID == id);
        if (tp == null)
        {
            if (ValueUtil.Get(value.GetType()).CompareEqual(value, (EntityUtilities.GetDefaultValue(value.GetType(),c.EntityPrefabID.combine(c.CompIdentity.right()), id)))==false)
            {
                c.Values.Add(new ValueBase(id, value));
                manager.DispathValueChangeMsg(new ValueChangeArgs(c.CompIdentity.left(),(c.CompIdentity.right()).combine(id),value),isRequest);
            }
            else
            {
                return false;
            }
        }
        else
        {
            if (ValueUtil.Get(value.GetType()).CompareEqual(value, tp.Data) == false)
            {
                tp.Data = value;
                manager.DispathValueChangeMsg(new ValueChangeArgs(c.CompIdentity.left(), (c.CompIdentity.right()).combine(id), value), isRequest);
            }
            else
            {
                return false;
            }
        }
        return true;
    }
    public static bool SetValue<T>(this ComponentData c, string name, T value,bool isRequest=false)
    {
        return c.SetValue(EntityUtilities.GetValueID(c, name), value,isRequest);
    }
    #endregion
}
